//
//  RetweetViewController.m
//  Guazi
//
//  Created by koolearn on 11-8-5.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "RetweetUsersViewController.h"
#import "RemarkInputCell.h"
#import "ImageStore.h"
#import "GuaziAppDelegate.h"
#import "FriendDetailViewController.h"

#define kRowsPerPage 50

@interface RetweetUsersViewController (private)
- (RemarkInputCell*) cellForRetweet:(UITableView *)tableView;
- (UITableViewCell*) cellForRetweetUser:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath;
- (void) loadRetweetUsers;
@end

@implementation RetweetUsersViewController

@synthesize status;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        users = [[NSMutableArray alloc] init];
    }
    return self;
}


- (id) initWithStatus:(Status*)astatus
{
    self = [super init];
    if ( self ) {
        self.status = astatus;
        users = [[NSMutableArray alloc] init];
    }
    return self;
}


- (void)dealloc
{
    [status release];
    [users release];
    [super dealloc];
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void) buildBarButtonItems
{
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"转发" style:UIBarButtonItemStyleDone target:self action:@selector(retweet)] autorelease];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    //[self buildBarButtonItems];
    [self loadRetweetUsers];
    self.tableView.backgroundColor = [UIColor colorWithRed:0.95f green:0.95f blue:0.95f alpha:1.0f];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.status = nil;
    [users release];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    if ( client ) {
        [client cancel];
        [client release];
        client = nil;
    }
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark - load data methods

- (void) loadRetweetUsers
{
    if ( client ) return;
    client = [[TwitterClient alloc] initWithTarget:self action:@selector(didLoadRetweetUsers:obj:)];
    [client retweetUsers:status.statusId pageNo:pageNo rowsPerPage:kRowsPerPage];
}


- (void) didLoadRetweetUsers:(id)sender obj:(NSObject*)obj
{
    client = nil;
    pageNo++;
    
    if ( obj == nil || obj == [NSNull null] ) return;
    if ( ![obj isKindOfClass:[NSArray class]] ) return;
    
    NSArray* arr = (NSArray*) obj;
    if ( [arr count] == 0 ) return;
    
    User* user ;
    for ( id theOne in arr ) {
        if ( theOne == nil ) continue;
        if ( ![theOne isKindOfClass:[NSDictionary class]] ) continue;
        NSDictionary* dic = (NSDictionary*) theOne;
        user = [User userWithJsonDictionary:dic];
        if ( user.userId <= 0 ) continue;
        
        [users addObject:user];
    }
    
    if ( [users count] > 0 ) {
        [self.tableView reloadData];
    }
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [users count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    User* user = [users objectAtIndex:indexPath.row];
    cell.textLabel.text = user.screenName;
    ImageStore* imageStore = [GuaziAppDelegate getAppDelegate].imageStore;
    cell.imageView.image = [imageStore getProfileImage:user.profileImageUrl_s isLarge:NO delegate:self];
    return cell;
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    User* user = [users objectAtIndex:row];
    FriendDetailViewController* controller = [[FriendDetailViewController alloc] initWithUser:user];
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

@end
